package com.benjaminwan.read.free.common

import com.benjaminwan.read.free.model.ItemOption

object KeyDefinition {
    val QWERTY_DEFINITION = listOf(
        ItemOption(0x29, "Esc"),
        ItemOption(0x3A, "F1"),
        ItemOption(0x3B, "F2"),
        ItemOption(0x3C, "F3"),
        ItemOption(0x3D, "F4"),
        ItemOption(0x3E, "F5"),
        ItemOption(0x3F, "F6"),
        ItemOption(0x40, "F7"),
        ItemOption(0x41, "F8"),
        ItemOption(0x42, "F9"),
        ItemOption(0x43, "F10"),
        ItemOption(0x44, "F11"),
        ItemOption(0x45, "F12"),
        ItemOption(0x4C, "Del"),
        ItemOption(0x35, "'~"),
        ItemOption(0x1E, "1!"),
        ItemOption(0x1F, "2@"),
        ItemOption(0x20, "3#"),
        ItemOption(0x21, "4$"),
        ItemOption(0x22, "5%"),
        ItemOption(0x23, "6^"),
        ItemOption(0x24, "7&"),
        ItemOption(0x25, "8*"),
        ItemOption(0x26, "9("),
        ItemOption(0x27, "0)"),
        ItemOption(0x2D, "-_"),
        ItemOption(0x2E, "=+"),
        ItemOption(0x2A, "Backspace←"),
        ItemOption(0x2B, "Tab↹"),
        ItemOption(0x14, "Q"),
        ItemOption(0x1A, "W"),
        ItemOption(0x08, "E"),
        ItemOption(0x15, "R"),
        ItemOption(0x17, "T"),
        ItemOption(0x1C, "Y"),
        ItemOption(0x18, "U"),
        ItemOption(0x0C, "I"),
        ItemOption(0x12, "O"),
        ItemOption(0x13, "P"),
        ItemOption(0x2F, "[{"),
        ItemOption(0x30, "]}"),
        ItemOption(0x31, "\\|"),
        ItemOption(0x39, "Caps Lock"),
        ItemOption(0x04, "A"),
        ItemOption(0x16, "S"),
        ItemOption(0x07, "D"),
        ItemOption(0x09, "F"),
        ItemOption(0x0A, "G"),
        ItemOption(0x0B, "H"),
        ItemOption(0x0D, "J"),
        ItemOption(0x0E, "K"),
        ItemOption(0x0F, "L"),
        ItemOption(0x33, ";:"),
        ItemOption(0x34, "'\""),
        ItemOption(0x28, "Enter↵"),
        ItemOption(0xE1, "Left Shift⇧"),
        ItemOption(0x1D, "Z"),
        ItemOption(0x1B, "X"),
        ItemOption(0x06, "C"),
        ItemOption(0x19, "V"),
        ItemOption(0x05, "B"),
        ItemOption(0x11, "N"),
        ItemOption(0x10, "M"),
        ItemOption(0x36, ",<"),
        ItemOption(0x37, ".>"),
        ItemOption(0x38, "/?"),
        ItemOption(0xE5, "Right Shift⇧"),
        ItemOption(0xE0, "Left Ctrl"),
        ItemOption(0xE3, "Left Win"),
        ItemOption(0xE2, "Left Alt"),
        ItemOption(0x2C, "Space"),
        ItemOption(0xE6, "Right Alt"),
        ItemOption(0xE7, "Right Win"),
        ItemOption(0x76, "Menu"),
        ItemOption(0xE4, "Right Ctrl")
    ).sortedBy { it.value }

    val NAVIGATION_DEFINITION = listOf(
        ItemOption(0x86, "Vol Down⇩"),
        ItemOption(0x84, "Mute"),
        ItemOption(0x85, "Vol Up⇧"),
        ItemOption(0x46, "Print Screen"),
        ItemOption(0x47, "Scroll Lock"),
        ItemOption(0x48, "Pause"),
        ItemOption(0x49, "Insert"),
        ItemOption(0x4A, "Home"),
        ItemOption(0x4B, "Page Up"),
        ItemOption(0x4C, "Delete"),
        ItemOption(0x4D, "End"),
        ItemOption(0x4E, "Page Down"),
        ItemOption(0x52, "↑"),
        ItemOption(0x50, "←"),
        ItemOption(0x51, "↓"),
        ItemOption(0x4F, "→")
    ).sortedBy { it.value }

    val NUMERIC_DEFINITION = listOf(
        ItemOption(0x80, "Prev⇦"),
        ItemOption(0x81, "Next⇨"),
        ItemOption(0x82, "Stop"),
        ItemOption(0x83, "Play Pause"),
        ItemOption(0x53, "Num Lock"),
        ItemOption(0x54, "/"),
        ItemOption(0x55, "*"),
        ItemOption(0x56, "-"),
        ItemOption(0x5F, "7Home"),
        ItemOption(0x60, "8↑"),
        ItemOption(0x61, "9PgUp"),
        ItemOption(0x57, "+"),
        ItemOption(0x5C, "4←"),
        ItemOption(0x5D, "5"),
        ItemOption(0x5E, "6→"),
        ItemOption(0x59, "1End"),
        ItemOption(0x5A, "2↓"),
        ItemOption(0x5B, "3PgDn"),
        ItemOption(0x62, "0Ins"),
        ItemOption(0x63, ".Del"),
        ItemOption(0x58, "Num Enter")
    ).sortedBy { it.value }

    val ALL_KEY_DEFINITION = mutableListOf<ItemOption>().apply {
        addAll(QWERTY_DEFINITION)
        addAll(NAVIGATION_DEFINITION)
        addAll(NUMERIC_DEFINITION)
    }

    fun findKeyName(key: Int, definition: List<ItemOption>): String {
        val keyDefine = definition.find { it.value == key }
        return keyDefine?.name ?: "非本区键值"
    }

    fun findKeyName(key: Int): String {
        val keyDefine = ALL_KEY_DEFINITION.find { it.value == key }
        return keyDefine?.name ?: "未知键值($key)"
    }
}